package com.cqrt.aop;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cqrt.service.RTProjectService;
import lombok.extern.slf4j.Slf4j;
import oracle.sql.CLOB;
import oracle.sql.TIMESTAMP;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.Reader;
import java.util.Map;

@Aspect
@Component
@Slf4j
public class QueryChangeIssueListByParamsFromProjectAOP {

    @Resource
    private RTProjectService rtProjectService;

//    @Around(value = "execution(* com.hustcad.plm.rpm.service.impl.issue.TyppmProjectChangeIssueLinkServiceImpl.queryChangeIssueListByParamsFromProject(..))")
    Object queryChangeIssueListByParamsFromProject(ProceedingJoinPoint joinPoint) throws Throwable {
        JSONObject jsonObject =(JSONObject) joinPoint.proceed();
        JSONArray list = jsonObject.getJSONArray("list");
        if (CollUtil.isNotEmpty(list)){
            for (int i = 0; i < list.size(); i++) {
                JSONObject json = list.getJSONObject(i);
                Map<String,Object> map=rtProjectService.selectDetsMeasures(json.getString("number"));
                if (map==null||map.isEmpty()){
                    return jsonObject;
                }
                if (map.get("phenomenonDescription")!=null){
                    map.put("phenomenonDescription",clobToString((CLOB)map.get("phenomenonDescription")));
                }
                if (map.get("longCountermeasures")!=null){
                    map.put("longCountermeasures",clobToString((CLOB)map.get("longCountermeasures")));
                }
                if (map.get("analysisDescription")!=null){
                    map.put("analysisDescription",clobToString((CLOB)map.get("analysisDescription")));
                }
                if (map.get("tentativeCountermeasures")!=null){
                    map.put("tentativeCountermeasures",clobToString((CLOB)map.get("tentativeCountermeasures")));
                }
                if (map.get("phenomenonFile")!=null){
                    map.put("phenomenonFile",clobToString((CLOB)map.get("phenomenonFile")));
                }
                if (map.get("videoFile")!=null){
                    map.put("videoFile",clobToString((CLOB)map.get("videoFile")));
                }

                if (map.get("occurrenceDate")!=null){
                    map.put("occurrenceDate",((TIMESTAMP)map.get("occurrenceDate")).stringValue());
                }
                if (map.get("confirmDate")!=null){
                    map.put("confirmDate",((TIMESTAMP)map.get("confirmDate")).stringValue());
                }
                if (map.get("plannedDate")!=null){
                    map.put("plannedDate",((TIMESTAMP)map.get("plannedDate")).stringValue());
                }
                if (map!=null&&!map.isEmpty()) {
                    json.putAll(map);
                }
            }
        }
        return jsonObject;
    }

    public static String clobToString(CLOB clob) {
        if (clob == null) {
            return null;
        }

        StringBuilder stringBuilder = new StringBuilder();
        try (Reader reader = clob.getCharacterStream();
             BufferedReader br = new BufferedReader(reader)) {
            String line;
            while ((line = br.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return stringBuilder.toString();
    }
}
